﻿function AzureOnlineBlobsDataContext(context) {
    var self = this;

    var container = ko.observable('');
    var containers = ko.observableArray([]);
    var blobs = ko.observableArray([]);

    var blobsSettings = ko.computed(function () {
        var as = context.accountSettings();
        return as.folder('blobs');
    });
    
    function addNewContainers(cnt) {
        var cnts = containers();
        cnts.push(cnt);
        cnts.sort(function (a, b) {
            var an = a.name();
            var bn = b.name();
            if (an > bn) return 1;
            if (an < bn) return -1;
            return 0;
        });
        containers(cnts);
    }

    function findByName(name) {
        var items = containers();
        for (var i = 0; i < items.length; i++) {
            var item = items[i];
            if (item.name() == name) return item;
        }
        return null;
    }

    function removeContainer(cnt) {
        containers.remove(cnt);
    }

    self.container = container;
    self.containers = containers;
    self.blobsSettings = blobsSettings;
    self.blobs = blobs;
    self.addNewContainers = addNewContainers;
    self.findByName = findByName;
    self.removeContainer = removeContainer;
}